package cn.edu.scau.cmi.yangqing.spring_hibernate.client;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.yangqing.spring_hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.yangqing.spring_hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.yangqing.spring_hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.spring_hibernate.domain.Teacher;
import cn.edu.scau.cmi.yangqing.spring_hibernate.util.ComprehensionApplicationContextUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class MyController implements Initializable {

	@FXML
	private TextField inputTeacherName;
	
	@FXML
	private Button saveTeacherButton;
	   
	@FXML
	private TextField inputStudentName;
		
	@FXML
	private Button saveStudentButton;
	
	@FXML
	private TextField outputTeacher;
	  
	@FXML
	private TextField outputStudent;
	
	ApplicationContext applicationContext = ComprehensionApplicationContextUtil.getApplicationContext();
	StudentDAO studentDAO = (StudentDAO) applicationContext.getBean("studentDAO");
	TeacherDAO teacherDAO = (TeacherDAO) applicationContext.getBean("teacherDAO");

	   
   public void initialize(URL location, ResourceBundle resources) {
	   }
	
   public void saveTeacher(ActionEvent event) {
	   Teacher teacher = new Teacher();
	   String name =inputTeacherName.getText() ;
	   teacher.setName(name);
	   teacherDAO.save(teacher);
	   
	   String outputTeacherStr = "输入的教师名字为："+name;
	   outputTeacher.setText(outputTeacherStr);
   }
   
   public void saveStudent(ActionEvent event) {
	    Student student = new Student();
	    String name = inputStudentName.getText();
	    student.setName(name);
	    Teacher teacherName = null;
	    List<Teacher> list = teacherDAO.findAll();
	    for (Teacher teacher : list) {
			if(teacher.getName().equals("梁早清")) {
				teacherName = teacher;
				System.out.println(teacher.getName());
				System.out.println(teacherName.getName());
			}
				
	    	
		}
	    
		student.setTeacher(teacherName);
		studentDAO.save(student);
		
		String outputStudentStr = "输入的学生名字为："+name+"  ";
		outputStudentStr += "他的老师为："+teacherName.getName();
		outputStudent.setText(outputStudentStr);
   }
	   
}
